package com.wawayaya.bookreader.bean;

// Notice: 
// The class describe its internal sorting mechanism which is INCONSISTENT with default 'equals' action

public class PosRegionKey implements Comparable<PosRegionKey>, java.io.Serializable {

	private static final long serialVersionUID = 1L;
	
	private RectSerial m_rs = null;
	
	
	public PosRegionKey(int ileft, int itop, int iright, int ibottom) {
		m_rs = new RectSerial(ileft,itop, iright, ibottom);
	}

	public PosRegionKey(RectSerial rs) {
		m_rs = rs;
	}
	
	public int getleft() {
		return m_rs.left;
	}
	public int gettop() {
		return m_rs.top;
	}
	public int getright() {
		return m_rs.right;
	}
	public int getbottom() {
		return m_rs.bottom;
	}	
	
	@Override
	public int compareTo(PosRegionKey r) {
		/*if(null == r) {
			return 1;
		}
		else if(Math.abs(gettop() - r.gettop()) <= 5 &&  Math.abs(getbottom() - r.getbottom()) <= 5) {
			if(getleft() > r.getleft() && getright() > r.getright()) {
				return 1;
			}
			else if(getleft() < r.getleft() && getright() < r.getright()) {
				return -1;
			}
			return 0;
		}
		else if(Math.abs(getleft() - r.getleft()) <= 5 &&  Math.abs(getright() - r.getright()) <= 5) {
			if(gettop() > r.gettop() && getbottom() > r.getbottom()) {
				return 1;
			}
			else if(gettop() < r.gettop() && getbottom() < r.getbottom()) {
				return -1;
			}
			return 0;
		}
		else{
			if(getleft() > r.getleft() && getright() > r.getright()) {
				return 1;
			}
			else if(getleft() < r.getleft() && getright() < r.getright()) {
				return -1;
			}
			else if(gettop() > r.gettop() && getbottom() > r.getbottom()) {
				return 1;
			}
			else if(gettop() < r.gettop() && getbottom() < r.getbottom()) {
				return -1;
			}
		}
		return 0;*/
		
    	
    	if(null == r) {
    		return 1;
    	}
    	else if(gettop() >= r.getbottom() || (r.getbottom() > gettop() && (r.getbottom() - gettop() <= 3))) {
    		return 1;
    	}
    	else if(getbottom() <= r.gettop() || (r.gettop() < getbottom() && (getbottom() - r.gettop() <= 3))) {
    		return -1;
    	}
    	else{
    		if(getleft() > r.getleft() && getright() > r.getright()) {
    			return 1;
    		}
    		else if(getleft() < r.getleft() && getright() < r.getright()) {
    			return -1;
    		}
    	}
    	return 0; 
	}
    
}
